package weaver.formmode.customjavacode;

import org.apache.commons.lang3.StringUtils;
import weaver.conn.RecordSet;
import weaver.formmode.setup.ModeRightInfo;
import weaver.general.Time;
import weaver.general.TimeUtil;
import weaver.general.Util;
import weaver.soa.workflow.request.RequestInfo;

import java.util.HashMap;
import java.util.Map;


/**
 * 员工宿舍登记保存后触发同步信息到宿舍信息建模
 *
 * @author Lee
 */
public class ResidentsExpand extends AbstractModeExpandJavaCodeNew {

    @Override
    public Map<String, String> doModeExpand(Map<String, Object> param) {
        Map<String, String> result = new HashMap<String, String>();
        try {
            RecordSet rs = new RecordSet();
            int billid = -1;
            int modeid = -1;
            RequestInfo requestInfo = (RequestInfo) param.get("RequestInfo");
            if (requestInfo != null) {
                billid = Util.getIntValue(requestInfo.getRequestid());
                modeid = Util.getIntValue(requestInfo.getWorkflowid());
                if (billid > 0 && modeid > 0) {
                    String currentDate = TimeUtil.getCurrentDateString();
                    String sql = "select a.id,a.kzrs,a.yzrs,b.rzsj,b.tssj from uf_ssxxwh_dt1 b " +
                            "left join uf_ssxxwh a on a.id=b.mainid " +
                            "where b.mainid=?";
                    rs.executeQuery(sql, billid);
                    //已住人数
                    int stay = 0;
                    while (rs.next()) {
                        String tssj = rs.getString("tssj");
                        //是否在住
                        if (StringUtils.isEmpty(tssj) || currentDate.compareTo(tssj) < 0) {
                            stay++;
                        }
                    }
                    rs.executeUpdate("update uf_ssxxwh set yzrs=? where id=?", stay, billid);
                }
            }
        } catch (Exception e) {
            result.put("errmsg", "自定义出错信息");
            result.put("flag", "false");
        }
        return result;
    }

}